package cn.demomaster.quickdb.mapper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.demomaster.quickdb.model.ColumnInfo;

public class MapperManager {
    Map<Class, CursorMapper> mapperMap;
    private static MapperManager instance;
    public static MapperManager getInstance(){
        if(instance == null) {
            synchronized (MapperManager.class) {
                if (instance == null) {
                    instance = new MapperManager();
                }
            }
        }
        return instance;
    }
    private MapperManager(){
        mapperMap = new HashMap<>();
       // mapperMap.put(SqliteTable.class,TestMapper.class);
    }

    public void addMapper(Class clazz,CursorMapper mapper){

        mapperMap.put(clazz,mapper);
    }
    public <T> CursorMapper getMapper(Class<T> clazz)
    {
        String bindClassName = clazz.getName() + "$$BindClass";
        try {
            // 加载生成的类
            Class mapperClass = Class.forName(bindClassName);

            // 检查是否是 CursorMapper 的子类
            if (CursorMapper.class.isAssignableFrom(mapperClass)) {
                // 创建实例并转换为 CursorMapper<T>
                @SuppressWarnings("unchecked")
                //CursorMapper<T> mapperInstance = (CursorMapper<T>) mapperClass.getDeclaredConstructor().newInstance();
                Constructor<?> constructor = mapperClass.getDeclaredConstructor();
                constructor.setAccessible(true); // 允许访问私有构造函数
                CursorMapper<T> mapperInstance = (CursorMapper<T>) constructor.newInstance();
                addMapper(clazz,mapperInstance);
            }
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
                 InstantiationException | InvocationTargetException e) {
            System.err.println("bindClassName="+bindClassName);
            throw new RuntimeException(e);
        }
        return mapperMap.get(clazz);
    }

    public <T> String getTableNameByClass(Class<T> clz1) {
        CursorMapper cursorMapper = getMapper(clz1);
        if (cursorMapper != null){
            return cursorMapper.getTableName();
        }
        return null;
    }

    public String getDatabaseNameByClass(Class<?> clz1,String tableName) {
        CursorMapper cursorMapper = getMapper(clz1);
        if (cursorMapper != null){
            return cursorMapper.getDataBaseName();
        }
        return null;
    }

    public String generateCreateTableSql(Class<?> clz1) {
        CursorMapper cursorMapper = getMapper(clz1);
        if (cursorMapper != null){
//            List<ColumnInfo> columnInfoList = cursorMapper.getColumnInfoList();
//            if(columnInfoList!=null&&columnInfoList.size()>0){
//                String sql = "CREATE TABLE if not exists "+getTableNameByClass(clz1)+"(";
//                boolean isFirst = true;
//                for (ColumnInfo columnInfo : columnInfoList) {
//                    if(isFirst){
//                        isFirst = false;
//                    }else {
//                        sql+=",";
//                    }
//                    sql+=columnInfo.getColumnName()+" "+columnInfo.getColumnType()+" "+columnInfo.getColumnComment();
//                }
//                sql+=")";
//                return sql;
//            }
            return cursorMapper.generateCreateTableSql();
        }
        return null;
    }
    public String generateInsertSql(Object obj) {
        CursorMapper cursorMapper = getMapper(obj.getClass());
        if (cursorMapper != null){
//            List<ColumnInfo> columnInfoList = cursorMapper.getColumnInfoList();
//            if(columnInfoList!=null&&columnInfoList.size()>0){
//                StringBuilder columnsStringBuilder = new StringBuilder();
//                StringBuilder valuesStringBuilder = new StringBuilder();
//                for (ColumnInfo columnInfo : columnInfoList) {
//                    columnsStringBuilder.append(columnInfo.getColumnName());
//                    valuesStringBuilder.append("?");
//                }
//                String sql = String.format("INSERT INTO TesBean(%s) VALUES(%s)", columnsStringBuilder, valuesStringBuilder);
//                return sql;
//            }
            return cursorMapper.generateInsertSql(obj);
        }
        return null;
    }
}
